﻿using MediatR;
using NPOI.SS.Formula.Functions;
using S.Authorization.API.Application.Command.UserCommand;
using S.Authorization.Common.Utilities;
using S.Authorization.Domain.Model.User;
using S.Authorization.Infrastructure.Interface.User;
using System;
using Yitter.IdGenerator;

namespace S.Authorization.API.Application.CommandHandlers.UserCommandHandler
{
    /// <summary>
    /// 人员添加
    /// </summary>
    public class UserCreatelCommondHandler : IRequestHandler<UserCreatelCommond, bool>
    {
        private readonly IUserModellRepository userRepository;

        public UserCreatelCommondHandler(IUserModellRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        public async Task<bool> Handle(UserCreatelCommond request, CancellationToken cancellationToken)
        {
            YitIdHelper.SetIdGenerator(new IdGeneratorOptions(0));
            string hash = Md5Helper.CalculateMd5Hash(request.UserPassword);
            //request.UserPassword = hash;
            UsersModel res = new UsersModel()
            {
                UserCode=YitIdHelper.NextId(),
                UserName= request.UserName,
                UserPassword = hash,
                UserNickName = "农友"+DateTime.Now.ToString("yyyyMMddHHmmss"),
                UserSex = request.UserSex,
                UserTrueName = request.UserTrueName,
                UserIDType = request.UserIDType,
                UserIdentityCard = request.UserIdentityCard,
                UserPhone = request.UserPhone,
                UserEmail = request.UserEmail,
                UserSchool = request.UserSchool,
                UserEducation = request.UserEducation,
                BusinessId=request.BusinessId,
                StaffState = request.StaffState,
                DepartmentId= request.DepartmentId,
                DateOnBoard = request.DateOnBoard,
                PositiveDates = request.PositiveDates,
                Province = request.Province,
                Area = request.Area,
                City=request.City,
                Address=request.Address,
            };
            return await userRepository.AddUserAndRole(res,request.RoleId);
        }
    }
}
